package com.net.oil.user;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.base.BaseActivity;
import com.net.oil.user.bean.CheckPhoneBean;
import com.net.oil.user.contract.ResetLoginPasswordContract;
import com.net.oil.user.presenter.ResetLoginPasswordPresenter;
import com.net.oil.util.StringUtil;
import com.net.oil.widget.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetLoginPasswordActivity extends BaseActivity<ResetLoginPasswordPresenter> implements ResetLoginPasswordContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.get_verify_code_button)
    TextView getVerifyCodeButton;
    @BindView(R.id.phone_number_et)
    EditText phoneNumberEt;
    @BindView(R.id.verify_code_et)
    EditText verifyCodeEt;
    @BindView(R.id.news_password_et)
    EditText newsPasswordEt;
    @BindView(R.id.confirm_password_et)
    EditText confirmPasswordEt;

    private long allTime = 60 * 1000;   //计时器时间
    private long stepTime = 1000;       //计时器每步的时间

    //计时器
    CountDownTimer timer = new CountDownTimer(allTime, stepTime) {
        @Override
        public void onTick(long millisUntilFinished) {
            getVerifyCodeButton.setText(String.format(getString(R.string.millis_format), millisUntilFinished / 1000));
            getVerifyCodeButton.setEnabled(false);
        }

        @Override
        public void onFinish() {
            getVerifyCodeButton.setText("重新获取验证码");
            getVerifyCodeButton.setEnabled(true);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_login_password);
        ButterKnife.bind(this);
        presenter = new ResetLoginPasswordPresenter(this);
        initView();
    }

    private void initView() {
        toolbarTitle.setText(R.string.reset_password);
    }

    @OnClick({R.id.toolbar_go_back, R.id.get_verify_code_button, R.id.reset_password_but})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
            case R.id.get_verify_code_button:
                checkPhone();
                break;
            case R.id.reset_password_but:
                verifyCode();
                break;
        }
    }
    /**
     * 验证手机号
     */
    private void checkPhone() {
        String phoneNumber = phoneNumberEt.getText().toString();
        if (!StringUtil.isEmpty(phoneNumber)){
            showProgressDialog();
            presenter.checkPhone(phoneNumber);
        }else {
            ToastUtil.showErrorToast(this, "请输入手机号");
        }
    }


    /**
     * 获取验证码
     */
    public void getPhoneCodeCode() {
        String phoneNumber = phoneNumberEt.getText().toString();
        if (!StringUtil.isEmpty(phoneNumber)) {
            timer.start();
            presenter.getPhoneCode(phoneNumber, false);
        }
    }

    /**
     * 验证验证码
     */
    private void verifyCode() {
        String phoneNumber = phoneNumberEt.getText().toString();
        String verifyCode = verifyCodeEt.getText().toString();
        if (!StringUtil.isEmpty(phoneNumber)) {
            if (!StringUtil.isEmpty(verifyCode)) {
                showProgressDialog();
                presenter.verifyCode(phoneNumber, verifyCode);
            } else {
                ToastUtil.showErrorToast(this, "请输入验证码");
            }
        } else {
            ToastUtil.showErrorToast(this, "请输入手机号");
        }
    }

    /**
     * 重置密码
     */
    private void resetPassword() {
        if (!StringUtil.isEmpty(phoneNumberEt.getText().toString())) {
            if (!StringUtil.isEmpty(newsPasswordEt.getText().toString())) {
                if (!StringUtil.isEmpty(confirmPasswordEt.getText().toString())) {
                    if (newsPasswordEt.getText().toString().length() < 6) {
                        ToastUtil.showErrorToast(this, "密码长度至少6位");
                    } else {
                        if (confirmPasswordEt.getText().toString().equals(newsPasswordEt.getText().toString())) {
                            showProgressDialog();
                            presenter.resetLoginPassword(phoneNumberEt.getText().toString(), verifyCodeEt.getText().toString(),
                                    newsPasswordEt.getText().toString());
                        } else {
                            ToastUtil.showErrorToast(this, "密码不一致");
                        }
                    }

                } else {
                    ToastUtil.showErrorToast(this, "请确认密码");
                }
            } else {
                ToastUtil.showErrorToast(this, "请输入密码");
            }
        } else {
            ToastUtil.showErrorToast(this, "请输入手机号");
        }
    }

    /**
     * 验证手机号成功
     *
     * @param checkPhoneBean 返回数据
     */
    @Override
    public void checkPhoneSuccess(CheckPhoneBean checkPhoneBean) {
        dismissProgressDialog();
        if (!checkPhoneBean.isExists()){
            ToastUtil.showErrorToast(this, "手机号不存在");
        }else {
            timer.start();
            getPhoneCodeCode();
        }
    }

    /**
     * 验证手机号失败
     */
    @Override
    public void checkPhoneFailed() {
        dismissProgressDialog();
        timer.cancel();
    }

    /**
     * 获取验证码成功
     */
    @Override
    public void getPhoneCodeSuccess() {

    }

    /**
     * 获取验证码失败
     */
    @Override
    public void getPhoneCodeFailed() {
        timer.cancel();
        timer.onFinish();
    }

    /**
     * 验证验证码成功
     */
    @Override
    public void verifyCodeSuccess() {
        dismissProgressDialog();
        resetPassword();
    }

    /**
     * 验证验证码失败
     */
    @Override
    public void verifyCodeFailed() {
        dismissProgressDialog();
    }

    /**
     * 重置登录密码成功
     */
    @Override
    public void resetLoginPasswordSuccess() {
        dismissProgressDialog();
        ToastUtil.showSuccessToast(this, "重置密码成功");
        finish();
    }

    /**
     * 重置登录密码失败
     */
    @Override
    public void resetLoginPasswordFailed() {
        dismissProgressDialog();
    }
}
